<template>
  <div class="pb24 uplaodBox">
    <a-row class="bbE8E8E8 h60 lh60 ">
      <a-col :span="24" class="exportTitle">
        <a-icon type="left" @click="goback" />
        <div class="left2" style="cursor:pointer;" @click="goback">返回</div>
        {{ name }}的{{ itemType }}
      </a-col>
    </a-row>
    <div class="pl24 pr24 pt24">
      <div class="tipBox pl24 pr24 ">
        <a-row class="fs16">温馨提示</a-row>
        <a-row>
          <a-col :span="12">
            <span class="color9 pr24"> 该材料为重要材料，上传重要材料可降低用工风险</span>
          </a-col>
        </a-row>
      </div>
    </div>
    <a-row style="padding:30px;display:flex;justify-content:flex-end">
      <a-button size="small" type="primary" @click="downloadImg" :disabled="edit != null">打包下载</a-button>
    </a-row>
    <div style="padding: 20px">
      <UploadSiteFile
        :disabled="edit != null"
        ref="uploadTool"
        :limit="9"
        :default-value="url"
        accept=".doc,.docx,.xls,.xlsx,.pdf,.zip,.rar,.jpg, .jpeg, .png"
        @change="onFileChange"
      />
    </div>
  </div>
</template>
<script>
import { getToken } from "xqjr-module-auth";
import { updateEmpAllInfo, getEmpInfoDetail } from "@/api/staff/staff";
import { UploadSiteFile } from "xqjr-plugin-common";
export default {
  components: {
    UploadSiteFile
  },

  data() {
    return {
      edit: null,
      id: "",
      name: "",
      fileType: 1,
      allList: [],
      itemType: "",
      currIndex: 0,
      currData: {},
      url: "",
      employInfoId: "",
      companyId: "",
      phone: ""
    };
  },
  created() {
    if (this.$route.query.edit !== undefined) {
      // 从员工概况进来的tag不为null
      this.edit = this.$route.query.edit;
    }
    this.employInfoId = this.$route.query.employInfoId;
    this.companyId = this.$route.query.companyId;
    const data = {
      id: this.employInfoId,
      companyId: this.companyId
    };
    this.id = this.$route.query.id;
    this.fileType = parseInt(this.$route.query.fileType);
    getEmpInfoDetail(data).then(res => {
      this.name = res.data.name;
      this.phone = res.data.phone;
      if (this.fileType === 1) {
        this.allList = JSON.parse(JSON.stringify(res.data.companyAdditionalFieldsListAttachmentVOList));
      } else if (this.fileType === 2) {
        this.allList = JSON.parse(JSON.stringify(res.data.companyAdditionalFieldsListArchiveVOList));
      } else {
        this.allList = JSON.parse(JSON.stringify(res.data.companyAdditionalFieldsListLeaveOfficeVOList));
      }
      this.allList.forEach((item, index) => {
        if (parseInt(item.id) === parseInt(this.id)) {
          this.currIndex = index;
          this.itemType = item.fieldName;
          this.currData = JSON.parse(JSON.stringify(item));
          this.url = this.currData.empAdditionalFieldsVO ? this.currData.empAdditionalFieldsVO.content : "";
        }
      });
    });
  },

  methods: {
    downloadImg() {
      this.$refs.uploadTool.download();
    },
    onFileChange(urls) {
      const data = {};
      const empData = {
        content: urls,
        companyId: this.companyId,
        empId: this.employInfoId,
        fieldId: this.currData.id,
        moduleCode: "archive"
      };
      if (this.currData.empAdditionalFieldsVO) {
        empData.id = this.currData.empAdditionalFieldsVO.id;
      }
      this.currData.empAdditionalFieldsVO = empData;
      this.allList[this.currIndex] = this.currData;
      if (this.fileType === 1) {
        data.companyAdditionalFieldsListAttachmentVOList = JSON.parse(JSON.stringify(this.allList));
      } else if (this.fileType === 2) {
        data.companyAdditionalFieldsListArchiveVOList = JSON.parse(JSON.stringify(this.allList));
      } else {
        data.companyAdditionalFieldsListLeaveOfficeVOList = JSON.parse(JSON.stringify(this.allList));
      }
      data.id = this.employInfoId;
      data.phone = this.phone;
      updateEmpAllInfo(data).then(res => {
        this.$notification["success"]({
          message: "操作成功"
        });
        this.$emit("cancelEdit");
      });
    },
    goback() {
      this.$router.go(-1);
    },
    pageGetToken() {
      return getToken();
    }
  }
};
</script>
<style lang="scss">
.tipBox {
  width: 100%;
  line-height: 35px;
  background: #faf9f9;
  border-radius: 4px;
  margin: 0 0 0 0;
}
.uplaodBox {
  .exportTitle {
    text-align: center;
    font-size: 16px;
    font-weight: 500;
    color: #252525;
    .left2 {
      z-index: 3;
      left: 50px;
      position: absolute;
      color: #595959;
      font-size: 16px;
    }
  }
  .anticon-left {
    z-index: 3;
    left: 24px;
    position: absolute;
    color: #d8d8d8;
    font-size: 26px;
    margin-top: 17px;
  }
  .ant-modal-body {
    line-height: 24px;

    em {
      color: var(--primary);
      font-style: normal;
    }
  }
}
.img-item {
  padding: 10px;
  border: 1px solid #e8e8e8;
  border-radius: 3px;
  margin-bottom: 10px;
  .imgpart {
    position: relative;
    width: 100%;
    height: 150px;
    overflow: hidden;
    &:hover {
      .shadow-box {
        display: flex;
      }
    }
    .shadow-box {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.5);
      display: none;
      justify-content: center;
      align-items: center;
      z-index: 1;
      i {
        width: 30px;
        height: 30px;
        background: rgba(255, 255, 255, 0.3);
        border-radius: 50%;
        font-size: 16px;
        color: #ffffff;
        line-height: 30px;
        text-align: center;
      }
    }
  }
  .img-info {
    display: flex;
    justify-content: space-between;
    i {
      margin-left: 5px;
    }
  }
}
</style>
